package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.dao.CategoryDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params, String type, Long catalogId) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
        if (type.equals("base")) {
            w.eq("attr_type",1);
        }else{
            w.eq("attr_type",0);
        }
        if (!catalogId.equals(0L)) {
            w.eq("catalog_id",catalogId);
        }
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            w.and(qw->
                    qw.like("attr_name",key).or().eq("attr_id",key)
            );
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );
        return new PageUtils(page);
    }

    @Override
    public List<Long> getCatalogPath(Long catalogId) {
        Long parentCid = categoryDao.selectById(catalogId).getParentCid();
        Long grandparentCid = categoryDao.selectById(parentCid).getParentCid();

        List<Long> list = new ArrayList<>();
        list.add(grandparentCid);
        list.add(parentCid);
        list.add(catalogId);

        return list;
    }

    @Override
    public List<AttrEntity> attrRelationByAttrGroupId(Long attrGroupId) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
        w.eq("attr_group_id",attrGroupId);
        w.eq("attr_type",1);
        return baseMapper.selectList(w);
    }

    @Override
    public PageUtils noattrRelation(Map<String, Object> params) {
        QueryWrapper<AttrEntity> w = new QueryWrapper<>();
        w.eq("attr_type",1);//规格参数
        w.isNull("attr_group_id");

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                w
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(AttrEntity entity) {
        Long catalogId = entity.getCatalogId();
        Integer attrGroupId = entity.getAttrGroupId();

        if (catalogId!=null){
            entity.setCatalogName(categoryDao.selectById(catalogId).getName());
        }
        if (attrGroupId!=null){
            entity.setAttrGroupName(attrGroupDao.selectById(attrGroupId).getAttrGroupName());
        }

        return super.save(entity);
    }
}
